home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / bit / src / ulib / vstrcat.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  2KB  |  76 lines

  1. /***********************************************************************
  2.  * $Id: vstrcat.c,v 0.80 1994/02/24 09:48:11 zhao Exp $
  3.  *
  4.  *.  Copyright(c) 1993,1994 by T.C. Zhao
  5.  *   All rights reserved.
  6.  *.
  7.  *   Similar to strcat, but takes variable number of argument
  8.  *
  9.  ***********************************************************************/
  10. #if !defined(lint) && defined(F_ID)
  11. char *id_vstrcat = "$Id: vstrcat.c,v 0.80 1994/02/24 09:48:11 zhao Exp $";
  12. #endif
  13.  
  14. #include <stdio.h>
  15. #include <stdarg.h>
  16. #include <string.h>
  17. #include <malloc.h>
  18. #include "ulib.h"
  19.  
  20. /* VARARGS1 */
  21. char *
  22. vstrcat(const char *s1,...)
  23. {
  24.     register size_t total = 0;
  25.     register char *ret, *p;
  26.     va_list ap;
  27.  
  28.     if (!s1)
  29.     return 0;
  30.  
  31.     total = strlen(s1);
  32.  
  33.     /* record total length */
  34.     va_start(ap, s1);
  35.     while ((p = va_arg(ap, char *)) != (char *) 0)
  36.       total += strlen(p);
  37.     va_end(ap);
  38.  
  39.  
  40.     if (!(ret = malloc(total + 1)))
  41.     return 0;
  42.  
  43.     strcpy(ret, s1);
  44.     va_start(ap, s1);
  45.     while ((p = va_arg(ap, char *)) != (char *) 0)
  46.       strcat(ret, p);
  47.     va_end(ap);
  48.     return ret;
  49. }
  50.  
  51. /****** so to protect from M_DBG *******/
  52. void
  53. free_vstrcat(void *p)
  54. {
  55.     free(p);
  56. }
  57.  
  58. #ifdef TEST
  59. #include <stdio.h>
  60. #include <string.h>
  61. #include "protolib.h"
  62. main()
  63. {
  64.     char *p = vstrcat("Hello", " World", "!", (char *) 0);
  65.     char *q = vstrcat(p, " again", " and again", (char *) 0);
  66.     char *l = vstrcat(q, " again", " and again", (char *) 0);
  67.     fprintf(stderr, "%s\n", p);
  68.     fprintf(stderr, "%s\n", q);
  69.     fprintf(stderr, "%s\n", l);
  70.     free(p);
  71.     free(q);
  72.     free(l);
  73. }
  74.  
  75. #endif
  76.